package com.pearadmin.pro.modules.ser.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.domain.Result;

import com.pearadmin.pro.modules.ser.domain.ServerInfo;
import com.pearadmin.pro.modules.ser.param.ServerInfoRequest;
import com.pearadmin.pro.modules.ser.service.ServerDeptService;
import com.pearadmin.pro.modules.ser.service.ServerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: didiplus
 * Email: 972479352@qq.com
 * CreateTime: 2022/3/5
 * Desc:
 */
@Api(tags = {"服务器信息"})
@RestController
@RequestMapping(ControllerConstant.PREFIX + "server")
public class ServerInfoController extends BaseController {


    @Resource
    ServerInfoService serverInfoService;

    @Resource
    ServerDeptService serverDeptService;

    @Resource
    private UserContext userContext;


    /**
     * 查询所有的服务器
     * @param  request 查询参数
     */
    @GetMapping("list")
    @Log(title = "服务器列表")
    @ApiOperation(value = "服务器列表")
    public Result list (ServerInfoRequest request){
        List<ServerInfo> serverList = serverInfoService.list(request);
        List list = serverDeptService.selectByDeptId(request.getDeptId());
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("server",serverList);
        objectMap.put("selectinfo",list);
        return success(objectMap);
    }

    /**
     * 查询服务器信息
     *
     * @param request 查询参数
     */
    @GetMapping("page")
    @Log(title = "服务列表")
    @ApiOperation(value = "服务列表")
    public Result page( ServerInfoRequest request){
        return success(serverInfoService.page(request));
    }




    /**
     * 修改字典类型
     *
     * @param serverInfo 字典实体
     */
    @PutMapping("edit")
    @Log(title = "修改服务信息")
    @ApiOperation(value = "修改服务信息")
    public Result edit(@RequestBody ServerInfo serverInfo) {
        System.out.println("serverInfo = " + serverInfo);
        return auto(serverInfoService.updateById(serverInfo));
    }

    /**
     * 新增字典值
     *
     * @param serverInfo 查询参数
     */
    @PostMapping("save")
    @Log(title = "新增服务数据")
    @ApiOperation(value = "新增服务数据")
    public Result save(@RequestBody ServerInfo serverInfo){
        return auto(serverInfoService.save(serverInfo));
    }


    /**
     * 删除字典类型
     *
     * @param id 服务器编号
     */
    @DeleteMapping("remove")
    @Log(title = "删除服务器")
    @ApiOperation(value = "删除服务器")
    public  Result remove(@RequestParam String id) {
        return auto(serverInfoService.removeById(id));
    }


    /**
     * 删除字典类型
     *
     * @param ids 字典实体
     */
    @DeleteMapping("removeBatch")
    @Log(title = "批量删除")
    @ApiOperation(value = "批量删除")
    public Result removeBatch(@RequestParam List<String> ids) {
        return auto(serverInfoService.removeByIds(ids));
    }


    /**
     * 根据登录用户所属部门获取所用的服务器列表并返回tree数据。
     */


    @GetMapping("tree")
    @Log(title = "获取部门所有的服务器")
    @ApiOperation(value = "获取部门所有的服务器")
    public Result tree() {
        return success( serverInfoService.getServerTree(userContext.getDeptId()));
    }
}
